<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Prepares an SQL statement to be executed</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.db2-prepare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">db2_prepare</h1>
  <p class="verinfo">(PECL ibm_db2 &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_prepare</span> &mdash; <span class="dc-title">
   Prepares an SQL statement to be executed
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.db2-prepare-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>db2_prepare</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$connection</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
  ] ) : <span class="type">resource</span></div>



  <p class="para rdfs-comment">
   <span class="function"><strong>db2_prepare()</strong></span> creates a prepared SQL statement which can
   include 0 or more parameter markers (<em>?</em> characters)
   representing parameters for input, output, or input/output. You can pass
   parameters to the prepared statement using
   <span class="function"><a href="db2_bind_param.html" class="function">db2_bind_param()</a></span>, or for input values only, as an array
   passed to <span class="function"><a href="db2_execute.html" class="function">db2_execute()</a></span>.
  </p>
  <p class="para">
   There are three main advantages to using prepared statements in your
   application:
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      <em class="emphasis">Performance</em>: when you prepare a statement, the
      database server creates an optimized access plan for retrieving data with
      that statement. Subsequently issuing the prepared statement with
      <span class="function"><a href="db2_execute.html" class="function">db2_execute()</a></span> enables the statements to reuse that
      access plan and avoids the overhead of dynamically creating a new access
      plan for every statement you issue.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <em class="emphasis">Security</em>: when you prepare a statement, you can
      include parameter markers for input values. When you execute a prepared
      statement with input values for placeholders, the database server checks
      each input value to ensure that the type matches the column definition or
      parameter definition.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <em class="emphasis">Advanced functionality</em>: Parameter markers not only
      enable you to pass input values to prepared SQL statements, they also
      enable you to retrieve OUT and INOUT parameters from stored procedures
      using <span class="function"><a href="db2_bind_param.html" class="function">db2_bind_param()</a></span>.
     </p>
    </li>
   </ul>
  </p>

 </div>

 <div class="refsect1 parameters" id="refsect1-function.db2-prepare-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">connection</code></dt>

     <dd>

      <p class="para">
       A valid database connection resource variable as returned from
       <span class="function"><a href="db2_connect.html" class="function">db2_connect()</a></span> or <span class="function"><a href="db2_pconnect.html" class="function">db2_pconnect()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">statement</code></dt>

     <dd>

      <p class="para">
       An SQL statement, optionally containing one or more parameter markers..
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       An associative array containing statement options. You can use this
       parameter to request a scrollable cursor on database servers that
       support this functionality.
      </p>
      <p class="para">
       For a description of valid statement options, see 
       <span class="function"><a href="db2_set_option.html" class="function">db2_set_option()</a></span>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.db2-prepare-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a statement resource if the SQL statement was successfully parsed and
   prepared by the database server. Returns <strong><code>FALSE</code></strong> if the database server
   returned an error. You can determine which error was returned by calling
   <span class="function"><a href="db2_stmt_error.html" class="function">db2_stmt_error()</a></span> or <span class="function"><a href="db2_stmt_errormsg.html" class="function">db2_stmt_errormsg()</a></span>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.db2-prepare-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1270">
    <p><strong>Example #1 Preparing and executing an SQL statement with parameter markers</strong></p>
    <div class="example-contents"><p>
     The following example prepares an INSERT statement that accepts four
     parameter markers, then iterates over an array of arrays containing the
     input values to be passed to <span class="function"><a href="db2_execute.html" class="function">db2_execute()</a></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$animals&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cat'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Pook'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3.2</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dog'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Peaches'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12.3</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'horse'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Smarty'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">350.0</span><span style="color: #007700">),<br />);<br /><br /></span><span style="color: #0000BB">$insert&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;animals&nbsp;(id,&nbsp;breed,&nbsp;name,&nbsp;weight)<br />&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(?,&nbsp;?,&nbsp;?,&nbsp;?)'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$insert</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$animals&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$animal</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$animal</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.db2-prepare-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="db2_bind_param.html" class="function" rel="rdfs-seeAlso">db2_bind_param()</a> - Binds a PHP variable to an SQL statement parameter</span></li>
    <li class="member"><span class="function"><a href="db2_execute.html" class="function" rel="rdfs-seeAlso">db2_execute()</a> - Executes a prepared SQL statement</span></li>
    <li class="member"><span class="function"><a href="db2_stmt_error.html" class="function" rel="rdfs-seeAlso">db2_stmt_error()</a> - Returns a string containing the SQLSTATE returned by an SQL statement</span></li>
    <li class="member"><span class="function"><a href="db2_stmt_errormsg.html" class="function" rel="rdfs-seeAlso">db2_stmt_errormsg()</a> - Returns a string containing the last SQL statement error message</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>